#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App qw(
NG::Gen::AuthCgi
);

use NG::Sys::Text;

my $t;
my $app = new NG::Sys::App();
$app->run;

sub on_prepare_loop {
	my ($app) = @_;
	$t = $app->template_tt->read("main");
}

sub on_before_process {
	my ($app,$form,$obj) = @_;
	unless($form->{user}){
		$form->redirect("/");
		return 0;
	}
	return 1;
}
sub cmd_default {
	my ($app,$form,$obj) = @_;
	$form->{data}{title} = $form->{user}->title;
	$form->{data}{login} = $form->{user}->login;
	$form->{data}{validate} = $form->{user}->validate;
	$form->process($t);
}
sub cmd_validate {
	my ($app,$form,$obj) = @_;
	unless($form->{user}->validate){
		if(my $err = $form->{user}->validate_user($form->{input}{login},$form->{input}{pass})){
			$form->set_error(100 => $err);
		}else{
			$form->{data}{msg} = "ok '$form->{input}{login}' '$form->{input}{pass}'";
		}
	}
	$app->do_action($form);
}
sub cmd_rename {
	my ($app,$form,$obj) = @_;
	if($form->{user}->validate){
		if(my $err = check_login_valid($form->{input}{login})){
			$form->set_error(100 => $err);
		}else{
			$form->{user}->title($form->{input}{login});
			$form->{data}{msg} = "ok '$form->{input}{login}'";
		}
	}
	$app->do_action($form);
}
